package com.smile.ye.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smile.ye.data.dto.CartOrderQueryDto;
import com.smile.ye.data.entity.CartDetailEntity;
import com.smile.ye.data.entity.CartOrderEntity;
import com.smile.ye.data.entity.ProductItemEntity;
import com.smile.ye.mapper.CartOrderMapper;
import com.smile.ye.mapper.ProductItemMapper;
import com.smile.ye.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Component
public class CartOrderService {
    @Autowired
    CartOrderMapper mapper;
    @Autowired
    ProductItemMapper productMapper;

    public Result query(CartOrderQueryDto dto){
        IPage<CartOrderEntity> page = new Page(dto.getPage(), dto.getSize());
        mapper.selectPage(page,null);
        return Result.build(page);
    }

    public Result queryById(String id){
        CartOrderEntity cartOrderEntity = mapper.selectById(id);
        return Result.build(cartOrderEntity);
    }

    public Result create(CartDetailEntity item) throws JsonProcessingException {
        if(item.getNum() ==0) throw new RuntimeException("商品数量不能为0");
        ProductItemEntity productItemEntity = productMapper.selectById(item.getItem());
        if(productItemEntity == null) throw new RuntimeException("商品不存在");
        HashSet<CartDetailEntity> products = new HashSet<>();
        products.add(item);
        CartOrderEntity cartOrderEntity = new CartOrderEntity();
        cartOrderEntity.setProducts(new ArrayList<>(products));
        cartOrderEntity.setTotalAmount(calculateTotalAmount(products));
        cartOrderEntity.setTotalCount(calculateCount(products));
        System.out.println(cartOrderEntity);
        mapper.insert(cartOrderEntity);
        return Result.build();
    }

    public Result refresh(String cart, CartDetailEntity item) throws JsonProcessingException {
        ProductItemEntity productItemEntity = productMapper.selectById(item.getItem());
        if(productItemEntity == null) throw new RuntimeException("商品不存在");
        CartOrderEntity cartOrderEntity = mapper.selectById(cart);
        HashSet<CartDetailEntity> products = formatSet(cartOrderEntity.getProducts());
        if(item.getNum() == 0) products.remove(item);
        else {
            if(products.contains(item)) products.remove(item);
            products.add(item);
        }
        cartOrderEntity.setProducts(new ArrayList<>(products));
        cartOrderEntity.setTotalCount(calculateCount(products));
        cartOrderEntity.setTotalAmount(calculateTotalAmount(products));
        int i = mapper.updateById(cartOrderEntity);
        if(i==0) throw new RuntimeException("该购物车不存在");
        return Result.build(cartOrderEntity);
    }

    public Result clear(String cart) throws JsonProcessingException {
        CartOrderEntity cartOrderEntity = mapper.selectById(cart);
        HashSet<CartDetailEntity> products = formatSet(cartOrderEntity.getProducts());
        products.clear();
        cartOrderEntity.setProducts(new ArrayList<>(products));
        int i = mapper.updateById(cartOrderEntity);
        if(i==0) throw new RuntimeException("该购物车不存在");
        return Result.build();
    }

    public Result remove(String id){
        int i = mapper.deleteById(id);
        System.out.println(i);
        if(i==0) throw new RuntimeException("该购物车不存在");
        return Result.build();
    }

    private  static Integer calculateTotalAmount(HashSet<CartDetailEntity> products){
        if(products.size() == 0) return 0;
         Integer price = 0;
        for(CartDetailEntity item : products){
            price+=item.getNum() * item.getPrice();
        }
        return price;
    }
    private static Integer calculateCount(HashSet<CartDetailEntity> products) {
        if(products.size() == 0) return 0;
        Integer count = 0;
        for(CartDetailEntity item : products){
            count+=item.getNum();
        }
        return count;
    }
    private static HashSet<CartDetailEntity> formatSet(List<CartDetailEntity> set) throws JsonProcessingException {
        String s = new ObjectMapper().writeValueAsString(set);
        List<CartDetailEntity> list= JSON.parseArray(s,CartDetailEntity.class);
        HashSet<CartDetailEntity> products = new HashSet<>(list);
        return products;
    }
}
